package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.common.TestDsl
import kotlin.math.max

/**
 * @author flutterdash@qq.com
 * @since 2021/7/9 13:35
 */
class P213B : TestDsl {

    private fun rob(nums: IntArray): Int {
        val arrSize = nums.size
        when (arrSize) {
            0 -> return 0
            1 -> return nums[0]
            2 -> return max(nums[0], nums[1])
        }

        val dp = IntArray(arrSize)

        fun rob(from: Int, to: Int): Int {
            dp[0] = 0
            dp[1] = nums[from]
            var dpPos = 2
            for (idx in (from + 1) until to) {
                dp[dpPos] = max(dp[dpPos - 2] + nums[idx], dp[dpPos - 1])
                dpPos++
            }
            return dp[arrSize - 1]
        }

        val value1 = rob(0, arrSize - 1)
        val value2 = rob(1, arrSize)
        return max(value1, value2)
    }

    @Test
    fun test0() {
        val input1 = intArrayOf(2, 3, 2)
        rob(input1) shouldBe 3

        val input2 = intArrayOf(1, 2, 3, 1)
        rob(input2) shouldBe 4

        val input3 = intArrayOf(0)
        rob(input3) shouldBe 0
    }

}
